home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-09-06 | 5.2 KB | 163 lines | [TEXT/MACA] |
- /*
- * piece.c - piece drawing routines for Tablut.
- *
- */
-
- #include <quickdraw.h>
- #include <window.h>
- #include <resource.h>
- #include <memory.h>
- #include "tablut.h"
-
- /*
- * getpieces() - get the pictures of the playing pieces
- */
-
- getpieces()
- {
- GrafPtr saveport;
- static GrafPort pieceport; /* (see flickport) */
- static BitMap flickmap; /* bitmap for pieceport */
- char *malloc();
-
-
- makepieces(&kingmaps, "king", THEKING, THEKING);
- makepieces(&swedmaps, "swede", FIRSTSWEDE, LASTSWEDE);
- makepieces(&muscmaps, "muscovite", FIRSTMUSC, LASTMUSC);
- squaredim.h = muscmaps.data->bounds.right + SQUARESEP;
- squaredim.v = muscmaps.data->bounds.bottom + SQUARESEP;
-
- flickport = &pieceport;
- SetRect(&flickmap.bounds, 0, 0, squaredim.h * 2, squaredim.v * 2);
- flickmap.rowBytes = (flickmap.bounds.right + 7) / 8;
- if (flickmap.rowBytes & 1) {
- ++(flickmap.rowBytes);
- }
- flickmap.baseAddr = (QDPtr) malloc((int) flickmap.rowBytes *
- flickmap.bounds.bottom);
- GetPort(&saveport);
- OpenPort(flickport);
- SetPortBits(&flickmap);
- SetPort(saveport);
- }
-
- /*
- * makepieces() - create the named class, then create the given pieces
- * of that class.
- */
-
- makepieces(class, name, first, last)
- struct piecebits *class; /* class to fill-in */
- char *name; /* (C) name of the class */
- int first, last; /* indices of the pieces to make */
- {
- PicHandle pic;
- char nambuf[100]; /* buffer for the changing name */
- struct pieceimage *p; /* piece to be made */
- char *malloc();
- BitMap *PictToMap();
-
- (void) strcpy(nambuf, name);
- (void) ctop(nambuf);
- pic = (PicHandle) GetNamedResource((long) 'PICT', nambuf);
- class->data = PictToMap(pic);
- ptoc(nambuf); (void) strcat(nambuf, "_mask"); ctop(nambuf);
- pic = (PicHandle) GetNamedResource((long) 'PICT', nambuf);
- class->mask = PictToMap(pic);
- class->etoc.h = class->data->bounds.right / 2;
- class->etoc.v = class->data->bounds.bottom / 2;
-
- for (p = &piece[first]; p <= &piece[last]; ++p) {
- p->class = class;
- p->prevlook.rowBytes = class->data->rowBytes;
- movmem((char *) &class->data->bounds, (char *) &p->prevlook.bounds,
- sizeof(Rect));
- OffsetRect(&p->prevlook.bounds, NOPLACE, NOPLACE);
- p->prevlook.baseAddr =
- (QDPtr) malloc((int) p->prevlook.rowBytes *
- class->data->bounds.bottom);
- p->grid.h = NOGRID;
- p->grid.v = NOGRID;
- p->curhome = &weirdhome;
- }
- }
-
- /*
- * drawpiece() - draw the given piece at the given location,
- * first removing it from its previous location (if any).
- * This routine assumes that the port is already set up.
- */
- drawpiece(idx, h, v)
- short h, v; /* center coords to move to */
- {
- struct pieceimage *p;
- Rect destrect; /* the rectangle that the piece will occupy */
- Rect affectrect; /* area affected by the move */
- Rect cliprect; /* the valid area to be copied */
- GrafPtr saveport;
- GrafPtr otherport;
-
- p = &piece[idx];
- h -= p->class->etoc.h;
- v -= p->class->etoc.v;
- if (h == p->prevlook.bounds.left && v == p->prevlook.bounds.top) {
- return; /* we're already there -- don't move */
- }
- movmem((char *) &p->prevlook.bounds, (char *) &destrect, sizeof(Rect));
- OffsetRect(&destrect, -destrect.left + h, -destrect.top + v);
- if (onscreen(p)) {
- if (SectRect(&p->prevlook.bounds, &destrect, &affectrect)) {
- UnionRect(&p->prevlook.bounds, &destrect, &affectrect);
- OffsetRect(&flickport->portBits.bounds,
- -(flickport->portBits.bounds.left) + affectrect.left,
- -(flickport->portBits.bounds.top) + affectrect.top);
- (void) SectRect(&thePort->portBits.bounds,
- &flickport->portBits.bounds, &cliprect);
- CopyBits(&thePort->portBits, &flickport->portBits,
- &cliprect, &cliprect, srcCopy, (RgnHandle) 0);
- GetPort(&saveport);
- SetPort(flickport);
- }
- (void) SectRect(&p->prevlook.bounds, &thePort->portBits.bounds,
- &cliprect);
- CopyBits(&p->prevlook, &thePort->portBits, &cliprect, &cliprect,
- srcCopy, (RgnHandle) 0);
- }
- OffsetRect(&p->prevlook.bounds,
- -(p->prevlook.bounds.left) + h, -(p->prevlook.bounds.top) + v);
- if (onscreen(p)) {
- (void) SectRect(&thePort->portBits.bounds, &p->prevlook.bounds,
- &cliprect);
- CopyBits(&thePort->portBits, &p->prevlook, &cliprect, &cliprect,
- srcCopy, (RgnHandle) 0);
- splatpiece((int)(p - &piece[0]));
- GetPort(&otherport);
- if (otherport == flickport) {
- SetPort(saveport);
- (void) SectRect(&flickport->portBits.bounds,
- &thePort->portBits.bounds, &cliprect);
- CopyBits(&flickport->portBits, &thePort->portBits,
- &cliprect, &cliprect, srcCopy, (RgnHandle) 0);
- }
- }
- }
-
- /*
- * splatpiece() - redraw the given piece at its current coordinates.
- * This routine assumes that the piece is on the screen somewhere.
- */
- splatpiece(pidx)
- int pidx; /* index of the piece to splat */
- {
- struct pieceimage *p;
- Rect cliprect; /* bounds of valid area to be copied */
-
- p = &piece[pidx];
- (void) SectRect(&p->prevlook.bounds, &thePort->portBits.bounds,
- &cliprect);
- CopyBits(p->class->mask, &thePort->portBits,
- &p->class->mask->bounds, &cliprect, srcBic, (RgnHandle) 0);
- CopyBits(p->class->data, &thePort->portBits,
- &p->class->data->bounds, &cliprect, srcXor, (RgnHandle) 0);
- }
-